<?php

/*
 * Echo's out the currently used template.
 * 
 * Use it only within the 'template_redirect' action (at this time WP will know the template).
 * For example: add_action('template_redirect', 'tyh_print_template');
 */
function tyh_print_template() {
    global $template;
	$template = basename( $template, '.php');
	var_dump( $template );
}

/*
 * Build a url with a query string for usage inside The Loop.
 * 
 * @param array $args (id => 'post_id', action => '')
 * 
 * @return string permalink with added query arguments
 */
function tyh_query_url( $args, $echo = TRUE )
{	
	$defaults = array( 'id' => 'post_id', 'action' => '', 'url' => 'permalink' );
	
	$args = wp_parse_args($args, $defaults);
	
	switch( $args['id'] )
	{
		case 'post_id':
			$args['id'] = get_the_ID(); // get current post id in the loop
			break;
	}
	
	// set the url
	$url = get_permalink();
	switch( $args['url'] )
	{
		case $args['url'] != 'permalink':
			$url = $args['url'];
			unset($args['url']);
			break;
	}
	
	$query_url = add_query_arg( $args, $url );	
	$query_url = esc_url( wp_nonce_url( $query_url, $args[ 'action' ] ) );
	
	if( $echo )
	{
		echo $query_url;
	}
	
	return $query_url;
}

/*
 * TODO: use a library to build a nice link OR search in WP if they have one already
 */
function tyh_get_link( $args ) 
{
	$defaults = array(
		'href' => '',
		'class' => '',
		'title' => '',
		'text' => ''
	);
	
	$args = wp_parse_args( $args, $defaults );
	
	$href = esc_url( $args['href'] );
	$href = "href=\"{$href}/\"";
	$class = "class=\"{$args['class']}\"";
	$title = esc_html( $args[ 'title' ] );
	$title = "title=\"{$title}\"";
	$text = esc_html( $args[ 'text' ] );
	
	$format = '<a %s %s %s>%s</a>';
	$link = sprintf( $format, $href, $class, $title, $text );
	
	return $link;
}